package org.lib.dbpoolhello;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.function.Consumer;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 检查DataSource
 * @author ben
 * @date 2021-09-28 16:50:22 CST
 */
@RestController
@RequestMapping(value="/ds/check")
public class CheckDatasource {

	private static Consumer<Object> cs = System.out::println;

	@Autowired
	private DataSource datasource;
	
	@GetMapping("/get")
	public Boolean get() {
		cs.accept("datasource=" + datasource);
		cs.accept("datasource.class=" + datasource.getClass());
		try {
			cs.accept("datasource.getLoginTimeout=" + datasource.getLoginTimeout());
		} catch (SQLException e) {
			cs.accept("getLoginTimeout() SQLException e=" + e.getClass());
		}
		try {
			Connection conn = datasource.getConnection();
			cs.accept("conn=" + conn);
			cs.accept("conn.Catalog=" + conn.getCatalog());
			cs.accept("conn.NetworkTimeout=" + conn.getNetworkTimeout());
			cs.accept("conn.Schema=" + conn.getSchema());
			cs.accept("conn.TransactionIsolation=" + conn.getTransactionIsolation());
			cs.accept("conn.ClientInfo=" + conn.getClientInfo());
			DatabaseMetaData md = conn.getMetaData();
			cs.accept("conn.md=" + md);
			cs.accept("conn.md.DriverName=" + md.getDriverName());
			cs.accept("conn.md.DriverVersion=" + md.getDriverVersion());
			cs.accept("conn.md.DatabaseProductName=" + md.getDatabaseProductName());
			cs.accept("conn.md.DatabaseProductVersion=" + md.getDatabaseProductVersion());
		} catch (SQLException e) {
			cs.accept("datasource.getConnection() SQLException e=" + e.getClass());
		}
		
		return true;
	}
	
}
